<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="机构" prop="tenantId">
		<el-select v-model="queryParams.tenantId" style="width: 200px">
		   <el-option
		      v-for="item in orgOptions"
		      :key="item.tenantId"
		      :label="item.companyName"
		      :value="item.tenantId"
		   />
		</el-select>
      </el-form-item>
     
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['system:orgRegion:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:orgRegion:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:orgRegion:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['system:orgRegion:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="orgRegionList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键id" align="center" prop="orgRegionId" />
      <el-table-column label="机构" align="center" prop="companyName" />
      <el-table-column label="省" align="center" prop="province" />
      <el-table-column label="市" align="center" prop="city" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['system:orgRegion:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['system:orgRegion:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改机构开通区域对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="orgRegionRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="机构" prop="tenantId">
           <el-select v-model="form.tenantId" style="width: 200px">
              <el-option
                 v-for="item in orgOptions"
                 :key="item.tenantId"
                 :label="item.companyName"
                 :value="item.tenantId"
              />
           </el-select>
        </el-form-item>
		<el-form-item class="form-item" label="区域" prop="citycode">
		    <el-cascader v-model="form.citycode" :options="cityOpt" :props="CascaderProps" />
		</el-form-item>
       <!-- <el-form-item label="省" prop="provice">
          <el-input v-model="form.provice" placeholder="请输入省" />
        </el-form-item>
        <el-form-item label="市" prop="city">
          <el-input v-model="form.city" placeholder="请输入市" />
        </el-form-item> -->
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="OrgRegion">
import { listOrgRegion, getOrgRegion, delOrgRegion, addOrgRegion, updateOrgRegion } from "@/api/system/orgRegion";
import { regionList } from "@/api/tool/region";

import { getOrgTenantList } from "@/api/system/tenant";
const { proxy } = getCurrentInstance();
const router = useRouter();
const orgRegionList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const orgOptions = ref([]);
const data = reactive({
	// 省市区 参数
  CascaderProps: {
	    value: 'name',
	    label: 'name',
	    children: 'list',
	},
  form: {},
  cityOpt: [],
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    tenantId: null,
    province: null,
    city: null,
  },
  rules: {
	  tenantId: [
	      { required: true, message: "机构不能为空", trigger: "blur" }
	  ],
	  citycode: [
	      { required: true, message: "公司地址不能为空", trigger: "blur" }
	  ],
  }
});

const { queryParams, form, rules,cityOpt,CascaderProps } = toRefs(data);

/** 查询机构开通区域列表 */
function getList() {
  loading.value = true;
  // console.log(router.query.orgId);
  listOrgRegion(queryParams.value).then(response => {
    orgRegionList.value = response.rows;
    total.value = Number(response.total);
    loading.value = false;
  });
}

// 所有省市区
function getRegionList() {
    regionList({ threeLevel: 2 }).then(response => {
        var data = response.data;
        cityOpt.value = data;
    });
}
// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    orgRegionId: null,
    tenantId: null,
    province: null,
    city: null,
    createTime: null
  };
  proxy.resetForm("orgRegionRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}
function getOrgList() {
    getOrgTenantList().then(response => {
    orgOptions.value = response.data;
  });
}
// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.orgRegionId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加机构开通区域";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _orgRegionId = row.orgRegionId || ids.value
  getOrgRegion(_orgRegionId).then(response => {
    form.value = response.data;
	form.value.citycode = [form.value.province, form.value.city];
	
    open.value = true;
    title.value = "修改机构开通区域";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["orgRegionRef"].validate(valid => {
    if (valid) {
		var formdata = JSON.parse(JSON.stringify(form.value));
		formdata.province = formdata.citycode[0];
		formdata.city = formdata.citycode[1];
      if (form.value.orgRegionId != null) {
        updateOrgRegion(formdata).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addOrgRegion(formdata).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _orgRegionIds = row.orgRegionId || ids.value;
  proxy.$modal.confirm('是否确认删除机构开通区域编号为"' + _orgRegionIds + '"的数据项？').then(function() {
    return delOrgRegion(_orgRegionIds);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('system/orgRegion/export', {
    ...queryParams.value
  }, `orgRegion_${new Date().getTime()}.xlsx`)
}

getList();
getOrgList();
getRegionList();
</script>
